package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.service.PaymentHystrixService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author：YH
 * @Description：
 * @Date:Created in 2021/5/3 19:19
 */
@RestController
@Slf4j
@DefaultProperties(defaultFallback = "payment_Global_FallbackMethod")   //全局服务降级。能够对带有注释的都执行一个服务降级方法
public class OrderHystrixController {

    @Resource
    private PaymentHystrixService paymentHystrixService;

    //http://localhost/consumer/payment/hystrix/ok/1
    @GetMapping(value = "/consumer/payment/hystrix/ok/{id}")
    public String paymentInfo_OK(@PathVariable("id") Integer id) {
        String result = paymentHystrixService.paymentInfo_OK(id);
        return result;
    }

    //设置服务自身可以调用时间的峰值限制，在峰值限制内返回正常操作，大于峰值限制的采用histrix进行服务的熔断降级
    //向调用方返回一个可处理的fallback


    @HystrixCommand
    //模拟较长时间的业务逻辑处理
 /*   @HystrixCommand(fallbackMethod = "paymentInfo_TimeOutHandler", commandProperties = {
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "2000")
    }) //3秒钟以内就是正常的业务逻辑*/
    @GetMapping(value = "/consumer/payment/hystrix/timeout/{id}")
    //http://localhost/consumer/payment/hystrix/timeout/1
    public String paymentInfo_TimeOut(@PathVariable("id") Integer id) {
        int a=10/0;
        String result = paymentHystrixService.paymentInfo_TimeOut(id);
        return result;
    }

    //兜底方法 上面方法出现问题的话采用handler方法
    public String paymentInfo_TimeOutHandler(@PathVariable("id") Integer id) {
        return "消费者允许调用生产者允许时间为1.5秒，feign调用超时请重新调用";
    }


    //全局异常处理机制，减少代码的耦合量
    public String payment_Global_FallbackMethod() {
        return "Global异常处理信息，请稍后再试";
    }
}
